﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DTS
{
    class HistoriaSpolki
    {
        List<Swieczka> dane;
        String symbol;

        public HistoriaSpolki(String _symbol)
        {
            symbol = _symbol;
            dane = new List<Swieczka>();
        }
        public HistoriaSpolki(String _symbol,List<Swieczka> _dane)
        {
            symbol = _symbol;
            dane = _dane;
        }
        public void addSwieczka(Swieczka toAdd)
        {
            dane.Add(toAdd);
        }
        public Swieczka getSwieczka(DateTime _data)
        {
            return dane.Find(swieczka => swieczka.GetData().Year == _data.Year && swieczka.GetData().Month == _data.Month && swieczka.GetData().Day == _data.Day);
        }
        public void sortujPoDacie()
        {
            dane.Sort((first,second) => -first.GetData().CompareTo(second.GetData()));
        }
        override public string ToString()
        {
            String tmp = "";
            foreach (Swieczka swc in dane)
            {
                tmp=tmp+swc.ToString()+"\n";
            }
            return tmp;
        }
        public List<Swieczka> getDane()
        {
            return dane;
        }
        public void setDane(List<Swieczka> _dane)
        {
            dane = _dane;
        }
        public HistoriaSpolki trim(DateTime dataStart, DateTime dataStop)
        {
            HistoriaSpolki toRet = new HistoriaSpolki(this.symbol);
            foreach (var tmp in this.dane.FindAll(swieczka => swieczka.GetData().Ticks >= dataStart.Ticks && swieczka.GetData().Ticks <= dataStop.Ticks))
            {
                Swieczka toAdd = new Swieczka();
                toAdd.SetData(tmp.GetData());
                toAdd.SetOpen(tmp.GetOpen());
                toAdd.SetMin(tmp.GetMin());
                toAdd.SetMax(tmp.GetMax());
                toAdd.SetClose(tmp.GetClose());
                toAdd.SetVolume(tmp.GetVolume());
                toRet.addSwieczka(toAdd);
            }
            return toRet;
        }
        public HistoriaSpolki getPrevOfData(DateTime dataStop, int ile)
        {
            HistoriaSpolki toRet = new HistoriaSpolki(this.symbol);
            int idx;
            do
            {
                idx = this.dane.IndexOf(dane.Find(s => s.GetData().Year == dataStop.Year && s.GetData().Month == dataStop.Month && s.GetData().Day == dataStop.Day));
                dataStop = dataStop.AddDays(-1);
            } while (idx == -1);
            
            for (int i = 0; i < ile;i++)
            {
                if (idx - i <= 0) break;
                var tmp = dane[idx - i];
                Swieczka toAdd = new Swieczka();
                toAdd.SetData(tmp.GetData());
                toAdd.SetOpen(tmp.GetOpen());
                toAdd.SetMin(tmp.GetMin());
                toAdd.SetMax(tmp.GetMax());
                toAdd.SetClose(tmp.GetClose());
                toAdd.SetVolume(tmp.GetVolume());
                toRet.addSwieczka(toAdd);
            }
            return toRet;
        }
        public bool czyDzienSesja(DateTime dataStop)
        {
            int idx = this.dane.IndexOf(dane.Find(s => s.GetData().Year == dataStop.Year && s.GetData().Month == dataStop.Month && s.GetData().Day == dataStop.Day));
            if (idx == -1) return false;
            return true;
        }
        public HistoriaSpolki kopiujTo()
        {
            HistoriaSpolki toRet = new HistoriaSpolki(this.symbol);
            foreach (var tmp in this.dane)
            {
                Swieczka toAdd = new Swieczka();
                toAdd.SetData(tmp.GetData());
                toAdd.SetOpen(tmp.GetOpen());
                toAdd.SetMin(tmp.GetMin());
                toAdd.SetMax(tmp.GetMax());
                toAdd.SetClose(tmp.GetClose());
                toAdd.SetVolume(tmp.GetVolume());
                toRet.addSwieczka(toAdd);
            }
            return toRet;
        }
    }
}
